home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / DebugGraphics.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  41.0 KB  |  1,282 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DebugGraphics.java    1.16 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.swing;
  16.  
  17. import java.awt.*;
  18. import java.awt.image.*;
  19.  
  20.  
  21. import java.text.AttributedCharacterIterator;
  22.  
  23.  
  24. /**
  25.  * Graphics subclass supporting graphics debugging. Overrides most methods
  26.  * from Graphics.  DebugGraphics objects are rarely created by hand.  They
  27.  * are most frequently created automatically when a JComponent's
  28.  * debugGraphicsOptions are changed using the setDebugGraphicsOptions()
  29.  * method.
  30.  * <p>
  31.  * NOTE: You must turn off double buffering to use DebugGraphics:
  32.  *       RepaintManager repaintManager = RepaintManager.currentManager(component);
  33.  *       repaintManager.setDoubleBufferingEnabled(false);
  34.  *
  35.  * @see JComponent#setDebugGraphicsOptions
  36.  * @see RepaintManager#currentManager
  37.  * @see RepaintManager#setDoubleBufferingEnabled
  38.  *
  39.  * @version 1.16 08/26/98
  40.  * @author Dave Karlton
  41.  */
  42. public class DebugGraphics extends Graphics {
  43.     Graphics                    graphics;
  44.     Image                       buffer;
  45.     int                         debugOptions;
  46.     int                         graphicsID = graphicsCount++;
  47.     int                         xOffset, yOffset;
  48.     private static int          graphicsCount = 0;
  49.  
  50.     /** Log graphics operations. */
  51.     public static final int     LOG_OPTION   = 1 << 0;
  52.     /** Flash graphics operations. */
  53.     public static final int     FLASH_OPTION = 1 << 1;
  54.     /** Show buffered operations in a seperate Frame. */
  55.     public static final int     BUFFERED_OPTION = 1 << 2;
  56.     /** Don't debug graphics operations. */
  57.     public static final int     NONE_OPTION = -1;
  58.  
  59.     /**
  60.      * Constructs a new debug graphics context that supports slowed
  61.      * down drawing.
  62.      */
  63.     public DebugGraphics() {
  64.         super();
  65.         buffer = null;
  66.         xOffset = yOffset = 0;
  67.     }
  68.  
  69.     /**
  70.      * Constructs a debug graphics context from an existing graphics
  71.      * context that slows down drawing for the specified component.
  72.      *
  73.      * @param graphics  the Graphics context to slow down
  74.      * @param component the JComponent to draw slowly
  75.      */
  76.     public DebugGraphics(Graphics graphics, JComponent component) {
  77.         this(graphics);
  78.         setDebugOptions(component.shouldDebugGraphics());
  79.     }
  80.  
  81.     /**
  82.      * Constructs a debug graphics context from an existing graphics
  83.      * context that supports slowed down drawing.
  84.      *
  85.      * @param graphics  the Graphics context to slow down
  86.      */
  87.     public DebugGraphics(Graphics graphics) {
  88.         this();
  89.         this.graphics = graphics;
  90.     }
  91.  
  92.     /**
  93.      * Overrides <code>Graphics.create</code> to return a DebugGraphics object.
  94.      */
  95.     public Graphics create() {
  96.         DebugGraphics debugGraphics;
  97.  
  98.         debugGraphics = new DebugGraphics();
  99.         debugGraphics.graphics = graphics.create();
  100.         debugGraphics.debugOptions = debugOptions;
  101.         debugGraphics.buffer = buffer;
  102.  
  103.         return debugGraphics;
  104.     }
  105.  
  106.     /**
  107.      * Overrides <code>Graphics.create</code> to return a DebugGraphics object.
  108.      */
  109.     public Graphics create(int x, int y, int width, int height) {
  110.         DebugGraphics debugGraphics;
  111.  
  112.         debugGraphics = new DebugGraphics();
  113.         debugGraphics.graphics = graphics.create(x, y, width, height);
  114.         debugGraphics.debugOptions = debugOptions;
  115.         debugGraphics.buffer = buffer;
  116.         debugGraphics.xOffset = xOffset + x;
  117.         debugGraphics.yOffset = yOffset + y;
  118.  
  119.         return debugGraphics;
  120.     }
  121.  
  122.  
  123.     //------------------------------------------------
  124.     //  NEW METHODS
  125.     //------------------------------------------------
  126.  
  127.     /** 
  128.      * Sets the Color used to flash drawing operations.
  129.      */
  130.     public static void setFlashColor(Color flashColor) {
  131.         info().flashColor = flashColor;
  132.     }
  133.  
  134.     /**
  135.      * Returns the Color used to flash drawing operations.
  136.      * @see #setFlashColor
  137.      */
  138.     public static Color flashColor() {
  139.         return info().flashColor;
  140.     }
  141.  
  142.     /**
  143.      * Sets the time delay of drawing operation flashing.
  144.      */
  145.     public static void setFlashTime(int flashTime) {
  146.         info().flashTime = flashTime;
  147.     }
  148.  
  149.     /**
  150.      * Returns the time delay of drawing operation flashing.
  151.      * @see #setFlashTime
  152.      */
  153.     public static int flashTime() {
  154.         return info().flashTime;
  155.     }
  156.  
  157.     /**
  158.      * Sets the number of times that drawing operations will flash.
  159.      */
  160.     public static void setFlashCount(int flashCount) {
  161.         info().flashCount = flashCount;
  162.     }
  163.  
  164.     /** Returns the number of times that drawing operations will flash.
  165.       * @see #setFlashCount
  166.       */
  167.     public static int flashCount() {
  168.         return info().flashCount;
  169.     }
  170.  
  171.     /** Sets the stream to which the DebugGraphics logs drawing operations.
  172.       */
  173.     public static void setLogStream(java.io.PrintStream stream) {
  174.         info().stream = stream;
  175.     }
  176.  
  177.     /** Returns the stream to which the DebugGraphics logs drawing operations.
  178.       * @see #setLogStream
  179.       */
  180.     public static java.io.PrintStream logStream() {
  181.         return info().stream;
  182.     }
  183.  
  184.     /** Sets the Font used for text drawing operations.
  185.       */
  186.     public void setFont(Font aFont) {
  187.         if (debugLog()) {
  188.             info().log(toShortString() + " Setting font: " + aFont);
  189.         }
  190.         graphics.setFont(aFont);
  191.     }
  192.  
  193.     /** Returns the Font used for text drawing operations.
  194.       * @see #setFont
  195.       */
  196.     public Font getFont() {
  197.         return graphics.getFont();
  198.     }
  199.  
  200.     /** Sets the color to be used for drawing and filling lines and shapes.
  201.       */
  202.     public void setColor(Color aColor) {
  203.         if (debugLog()) {
  204.             info().log(toShortString() + " Setting color: " + aColor);
  205.         }
  206.         graphics.setColor(aColor);
  207.     }
  208.  
  209.     /** Returns the Color used for text drawing operations.
  210.       * @see #setColor
  211.       */
  212.     public Color getColor() {
  213.         return graphics.getColor();
  214.     }
  215.  
  216.  
  217.     //-----------------------------------------------
  218.     // OVERRIDDEN METHODS
  219.     //------------------------------------------------
  220.  
  221.     /**
  222.      * Overrides <code>Graphics.getFontMetrics</code>.
  223.      */
  224.     public FontMetrics getFontMetrics() {
  225.         return graphics.getFontMetrics();
  226.     }
  227.  
  228.     /**
  229.      * Overrides <code>Graphics.getFontMetrics</code>.
  230.      */
  231.     public FontMetrics getFontMetrics(Font f) {
  232.         return graphics.getFontMetrics(f);
  233.     }
  234.  
  235.     /**
  236.      * Overrides <code>Graphics.translate</code>.
  237.      */
  238.     public void translate(int x, int y) {
  239.         if (debugLog()) {
  240.             info().log(toShortString() +
  241.                 " Translating by: " + new Point(x, y));
  242.         }
  243.         xOffset += x;
  244.         yOffset += y;
  245.         graphics.translate(x, y);
  246.     }
  247.  
  248.     /**
  249.      * Overrides <code>Graphics.setPaintMode</code>.
  250.      */
  251.     public void setPaintMode() {
  252.         if (debugLog()) {
  253.             info().log(toShortString() + " Setting paint mode");
  254.         }
  255.         graphics.setPaintMode();
  256.     }
  257.  
  258.     /**
  259.      * Overrides <code>Graphics.setXORMode</code>.
  260.      */
  261.     public void setXORMode(Color aColor) {
  262.         if (debugLog()) {
  263.             info().log(toShortString() + " Setting XOR mode: " + aColor);
  264.         }
  265.         graphics.setXORMode(aColor);
  266.     }
  267.  
  268.     /**
  269.      * Overrides <code>Graphics.getClipBounds</code>.
  270.      */
  271.     public Rectangle getClipBounds() {
  272.         return graphics.getClipBounds();
  273.     }
  274.  
  275.     /**
  276.      * Overrides <code>Graphics.clipRect</code>.
  277.      */
  278.     public void clipRect(int x, int y, int width, int height) {
  279.         graphics.clipRect(x, y, width, height);
  280.         if (debugLog()) {
  281.             info().log(toShortString() +
  282.                 " Setting clipRect: " + (new Rectangle(x, y, width, height)) +
  283.                 " New clipRect: " + graphics.getClip());
  284.         }
  285.     }
  286.  
  287.     /**
  288.      * Overrides <code>Graphics.setClip</code>.
  289.      */
  290.     public void setClip(int x, int y, int width, int height) {
  291.         graphics.setClip(x, y, width, height);
  292.         if (debugLog()) {
  293.             info().log(toShortString() +
  294.                         " Setting new clipRect: " + graphics.getClip());
  295.         }
  296.     }
  297.  
  298.     /**
  299.      * Overrides <code>Graphics.getClip</code>.
  300.      */
  301.     public Shape getClip() {
  302.         return graphics.getClip();
  303.     }
  304.  
  305.     /**
  306.      * Overrides <code>Graphics.setClip</code>.
  307.      */
  308.     public void setClip(Shape clip) {
  309.         graphics.setClip(clip);
  310.         if (debugLog()) {
  311.             info().log(toShortString() +
  312.                        " Setting new clipRect: " +  graphics.getClip());
  313.         }
  314.     }
  315.  
  316.     /**
  317.      * Overrides <code>Graphics.drawRect</code>.
  318.      */
  319.     public void drawRect(int x, int y, int width, int height) {
  320.         DebugGraphicsInfo info = info();
  321.  
  322.         if (debugLog()) {
  323.             info().log(toShortString() +
  324.                       " Drawing rect: " +
  325.                       new Rectangle(x, y, width, height));
  326.         }
  327.  
  328.         if (isDrawingBuffer()) {
  329.             if (debugBuffered()) {
  330.                 Graphics debugGraphics = debugGraphics();
  331.  
  332.                 debugGraphics.drawRect(x, y, width, height);
  333.                 debugGraphics.dispose();
  334.             }
  335.         } else if (debugFlash()) {
  336.             Color oldColor = getColor();
  337.             int i, count = (info.flashCount * 2) - 1;
  338.  
  339.             for (i = 0; i < count; i++) {
  340.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  341.                 graphics.drawRect(x, y, width, height);
  342.                 Toolkit.getDefaultToolkit().sync();
  343.                 sleep(info.flashTime);
  344.             }
  345.             graphics.setColor(oldColor);
  346.         }
  347.         graphics.drawRect(x, y, width, height);
  348.     }
  349.  
  350.     /**
  351.      * Overrides <code>Graphics.fillRect</code>.
  352.      */
  353.     public void fillRect(int x, int y, int width, int height) {
  354.         DebugGraphicsInfo info = info();
  355.  
  356.         if (debugLog()) {
  357.             info().log(toShortString() +
  358.                       " Filling rect: " +
  359.                       new Rectangle(x, y, width, height));
  360.         }
  361.  
  362.         if (isDrawingBuffer()) {
  363.             if (debugBuffered()) {
  364.                 Graphics debugGraphics = debugGraphics();
  365.  
  366.                 debugGraphics.fillRect(x, y, width, height);
  367.                 debugGraphics.dispose();
  368.             }
  369.         } else if (debugFlash()) {
  370.             Color oldColor = getColor();
  371.             int i, count = (info.flashCount * 2) - 1;
  372.  
  373.             for (i = 0; i < count; i++) {
  374.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  375.                 graphics.fillRect(x, y, width, height);
  376.                 Toolkit.getDefaultToolkit().sync();
  377.                 sleep(info.flashTime);
  378.             }
  379.             graphics.setColor(oldColor);
  380.         }
  381.         graphics.fillRect(x, y, width, height);
  382.     }
  383.  
  384.     /**
  385.      * Overrides <code>Graphics.clearRect</code>.
  386.      */
  387.     public void clearRect(int x, int y, int width, int height) {
  388.         DebugGraphicsInfo info = info();
  389.  
  390.         if (debugLog()) {
  391.             info().log(toShortString() +
  392.                       " Clearing rect: " +
  393.                       new Rectangle(x, y, width, height));
  394.         }
  395.  
  396.         if (isDrawingBuffer()) {
  397.             if (debugBuffered()) {
  398.                 Graphics debugGraphics = debugGraphics();
  399.  
  400.                 debugGraphics.clearRect(x, y, width, height);
  401.                 debugGraphics.dispose();
  402.             }
  403.         } else if (debugFlash()) {
  404.             Color oldColor = getColor();
  405.             int i, count = (info.flashCount * 2) - 1;
  406.  
  407.             for (i = 0; i < count; i++) {
  408.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  409.                 graphics.clearRect(x, y, width, height);
  410.                 Toolkit.getDefaultToolkit().sync();
  411.                 sleep(info.flashTime);
  412.             }
  413.             graphics.setColor(oldColor);
  414.         }
  415.         graphics.clearRect(x, y, width, height);
  416.     }
  417.  
  418.     /**
  419.      * Overrides <code>Graphics.drawRoundRect</code>.
  420.      */
  421.     public void drawRoundRect(int x, int y, int width, int height,
  422.                               int arcWidth, int arcHeight) {
  423.         DebugGraphicsInfo info = info();
  424.  
  425.         if (debugLog()) {
  426.             info().log(toShortString() +
  427.                       " Drawing round rect: " +
  428.                       new Rectangle(x, y, width, height) +
  429.                       " arcWidth: " + arcWidth +
  430.                       " archHeight: " + arcHeight);
  431.         }
  432.         if (isDrawingBuffer()) {
  433.             if (debugBuffered()) {
  434.                 Graphics debugGraphics = debugGraphics();
  435.  
  436.                 debugGraphics.drawRoundRect(x, y, width, height,
  437.                                             arcWidth, arcHeight);
  438.                 debugGraphics.dispose();
  439.             }
  440.         } else if (debugFlash()) {
  441.             Color oldColor = getColor();
  442.             int i, count = (info.flashCount * 2) - 1;
  443.  
  444.             for (i = 0; i < count; i++) {
  445.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  446.                 graphics.drawRoundRect(x, y, width, height,
  447.                                        arcWidth, arcHeight);
  448.                 Toolkit.getDefaultToolkit().sync();
  449.                 sleep(info.flashTime);
  450.             }
  451.             graphics.setColor(oldColor);
  452.         }
  453.         graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
  454.     }
  455.  
  456.     /**
  457.      * Overrides <code>Graphics.fillRoundRect</code>.
  458.      */
  459.     public void fillRoundRect(int x, int y, int width, int height,
  460.                               int arcWidth, int arcHeight) {
  461.         DebugGraphicsInfo info = info();
  462.  
  463.         if (debugLog()) {
  464.             info().log(toShortString() +
  465.                       " Filling round rect: " +
  466.                       new Rectangle(x, y, width, height) +
  467.                       " arcWidth: " + arcWidth +
  468.                       " archHeight: " + arcHeight);
  469.         }
  470.         if (isDrawingBuffer()) {
  471.             if (debugBuffered()) {
  472.                 Graphics debugGraphics = debugGraphics();
  473.  
  474.                 debugGraphics.fillRoundRect(x, y, width, height,
  475.                                             arcWidth, arcHeight);
  476.                 debugGraphics.dispose();
  477.             }
  478.         } else if (debugFlash()) {
  479.             Color oldColor = getColor();
  480.             int i, count = (info.flashCount * 2) - 1;
  481.  
  482.             for (i = 0; i < count; i++) {
  483.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  484.                 graphics.fillRoundRect(x, y, width, height,
  485.                                        arcWidth, arcHeight);
  486.                 Toolkit.getDefaultToolkit().sync();
  487.                 sleep(info.flashTime);
  488.             }
  489.             graphics.setColor(oldColor);
  490.         }
  491.         graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
  492.     }
  493.  
  494.     /**
  495.      * Overrides <code>Graphics.drawLine</code>.
  496.      */
  497.     public void drawLine(int x1, int y1, int x2, int y2) {
  498.         DebugGraphicsInfo info = info();
  499.  
  500.         if (debugLog()) {
  501.             info().log(toShortString() +
  502.                        " Drawing line: from " + pointToString(x1, y1) +
  503.                        " to " +  pointToString(x2, y2));
  504.         }
  505.  
  506.         if (isDrawingBuffer()) {
  507.             if (debugBuffered()) {
  508.                 Graphics debugGraphics = debugGraphics();
  509.  
  510.                 debugGraphics.drawLine(x1, y1, x2, y2);
  511.                 debugGraphics.dispose();
  512.             }
  513.         } else if (debugFlash()) {
  514.             Color oldColor = getColor();
  515.             int i, count = (info.flashCount * 2) - 1;
  516.  
  517.             for (i = 0; i < count; i++) {
  518.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  519.                 graphics.drawLine(x1, y1, x2, y2);
  520.                 Toolkit.getDefaultToolkit().sync();
  521.                 sleep(info.flashTime);
  522.             }
  523.             graphics.setColor(oldColor);
  524.         }
  525.         graphics.drawLine(x1, y1, x2, y2);
  526.     }
  527.  
  528.     /**
  529.      * Overrides <code>Graphics.draw3DRect</code>.
  530.      */
  531.     public void draw3DRect(int x, int y, int width, int height,
  532.                            boolean raised) {
  533.         DebugGraphicsInfo info = info();
  534.  
  535.         if (debugLog()) {
  536.             info().log(toShortString() +
  537.                        " Drawing 3D rect: " +
  538.                        new Rectangle(x, y, width, height) +
  539.                        " Raised bezel: " + raised);
  540.         }
  541.         if (isDrawingBuffer()) {
  542.             if (debugBuffered()) {
  543.                 Graphics debugGraphics = debugGraphics();
  544.  
  545.                 debugGraphics.draw3DRect(x, y, width, height, raised);
  546.                 debugGraphics.dispose();
  547.             }
  548.         } else if (debugFlash()) {
  549.             Color oldColor = getColor();
  550.             int i, count = (info.flashCount * 2) - 1;
  551.  
  552.             for (i = 0; i < count; i++) {
  553.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  554.                 graphics.draw3DRect(x, y, width, height, raised);
  555.                 Toolkit.getDefaultToolkit().sync();
  556.                 sleep(info.flashTime);
  557.             }
  558.             graphics.setColor(oldColor);
  559.         }
  560.         graphics.draw3DRect(x, y, width, height, raised);
  561.     }
  562.  
  563.     /**
  564.      * Overrides <code>Graphics.fill3DRect</code>.
  565.      */
  566.     public void fill3DRect(int x, int y, int width, int height,
  567.                boolean raised) {
  568.         DebugGraphicsInfo info = info();
  569.  
  570.         if (debugLog()) {
  571.             info().log(toShortString() +
  572.                        " Filling 3D rect: " +
  573.                        new Rectangle(x, y, width, height) +
  574.                        " Raised bezel: " + raised);
  575.         }
  576.         if (isDrawingBuffer()) {
  577.             if (debugBuffered()) {
  578.                 Graphics debugGraphics = debugGraphics();
  579.  
  580.                 debugGraphics.fill3DRect(x, y, width, height, raised);
  581.                 debugGraphics.dispose();
  582.             }
  583.         } else if (debugFlash()) {
  584.             Color oldColor = getColor();
  585.             int i, count = (info.flashCount * 2) - 1;
  586.  
  587.             for (i = 0; i < count; i++) {
  588.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  589.                 graphics.fill3DRect(x, y, width, height, raised);
  590.                 Toolkit.getDefaultToolkit().sync();
  591.                 sleep(info.flashTime);
  592.             }
  593.             graphics.setColor(oldColor);
  594.         }
  595.         graphics.fill3DRect(x, y, width, height, raised);
  596.     }
  597.  
  598.     /**
  599.      * Overrides <code>Graphics.drawOval</code>.
  600.      */
  601.     public void drawOval(int x, int y, int width, int height) {
  602.         DebugGraphicsInfo info = info();
  603.  
  604.         if (debugLog()) {
  605.             info().log(toShortString() +
  606.                       " Drawing oval: " +
  607.                       new Rectangle(x, y, width, height));
  608.         }
  609.         if (isDrawingBuffer()) {
  610.             if (debugBuffered()) {
  611.                 Graphics debugGraphics = debugGraphics();
  612.  
  613.                 debugGraphics.drawOval(x, y, width, height);
  614.                 debugGraphics.dispose();
  615.             }
  616.         } else if (debugFlash()) {
  617.             Color oldColor = getColor();
  618.             int i, count = (info.flashCount * 2) - 1;
  619.  
  620.             for (i = 0; i < count; i++) {
  621.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  622.                 graphics.drawOval(x, y, width, height);
  623.                 Toolkit.getDefaultToolkit().sync();
  624.                 sleep(info.flashTime);
  625.             }
  626.             graphics.setColor(oldColor);
  627.         }
  628.         graphics.drawOval(x, y, width, height);
  629.     }
  630.  
  631.     /**
  632.      * Overrides <code>Graphics.fillOval</code>.
  633.      */
  634.     public void fillOval(int x, int y, int width, int height) {
  635.         DebugGraphicsInfo info = info();
  636.  
  637.         if (debugLog()) {
  638.             info().log(toShortString() +
  639.                       " Filling oval: " +
  640.                       new Rectangle(x, y, width, height));
  641.         }
  642.         if (isDrawingBuffer()) {
  643.             if (debugBuffered()) {
  644.                 Graphics debugGraphics = debugGraphics();
  645.  
  646.                 debugGraphics.fillOval(x, y, width, height);
  647.                 debugGraphics.dispose();
  648.             }
  649.         } else if (debugFlash()) {
  650.             Color oldColor = getColor();
  651.             int i, count = (info.flashCount * 2) - 1;
  652.  
  653.             for (i = 0; i < count; i++) {
  654.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  655.                 graphics.fillOval(x, y, width, height);
  656.                 Toolkit.getDefaultToolkit().sync();
  657.                 sleep(info.flashTime);
  658.             }
  659.             graphics.setColor(oldColor);
  660.         }
  661.         graphics.fillOval(x, y, width, height);
  662.     }
  663.  
  664.     /**
  665.      * Overrides <code>Graphics.drawArc</code>.
  666.      */
  667.     public void drawArc(int x, int y, int width, int height,
  668.                         int startAngle, int arcAngle) {
  669.         DebugGraphicsInfo info = info();
  670.  
  671.         if (debugLog()) {
  672.             info().log(toShortString() +
  673.                       " Drawing arc: " +
  674.                       new Rectangle(x, y, width, height) +
  675.                       " startAngle: " + startAngle +
  676.                       " arcAngle: " + arcAngle);
  677.         }
  678.         if (isDrawingBuffer()) {
  679.             if (debugBuffered()) {
  680.                 Graphics debugGraphics = debugGraphics();
  681.  
  682.                 debugGraphics.drawArc(x, y, width, height,
  683.                                       startAngle, arcAngle);
  684.                 debugGraphics.dispose();
  685.             }
  686.         } else if (debugFlash()) {
  687.             Color oldColor = getColor();
  688.             int i, count = (info.flashCount * 2) - 1;
  689.  
  690.             for (i = 0; i < count; i++) {
  691.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  692.                 graphics.drawArc(x, y, width, height, startAngle, arcAngle);
  693.                 Toolkit.getDefaultToolkit().sync();
  694.                 sleep(info.flashTime);
  695.             }
  696.             graphics.setColor(oldColor);
  697.         }
  698.         graphics.drawArc(x, y, width, height, startAngle, arcAngle);
  699.     }
  700.  
  701.     /**
  702.      * Overrides <code>Graphics.fillArc</code>.
  703.      */
  704.     public void fillArc(int x, int y, int width, int height,
  705.                         int startAngle, int arcAngle) {
  706.         DebugGraphicsInfo info = info();
  707.  
  708.         if (debugLog()) {
  709.             info().log(toShortString() +
  710.                       " Filling arc: " +
  711.                       new Rectangle(x, y, width, height) +
  712.                       " startAngle: " + startAngle +
  713.                       " arcAngle: " + arcAngle);
  714.         }
  715.         if (isDrawingBuffer()) {
  716.             if (debugBuffered()) {
  717.                 Graphics debugGraphics = debugGraphics();
  718.  
  719.                 debugGraphics.fillArc(x, y, width, height,
  720.                                       startAngle, arcAngle);
  721.                 debugGraphics.dispose();
  722.             }
  723.         } else if (debugFlash()) {
  724.             Color oldColor = getColor();
  725.             int i, count = (info.flashCount * 2) - 1;
  726.  
  727.             for (i = 0; i < count; i++) {
  728.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  729.                 graphics.fillArc(x, y, width, height, startAngle, arcAngle);
  730.                 Toolkit.getDefaultToolkit().sync();
  731.                 sleep(info.flashTime);
  732.             }
  733.             graphics.setColor(oldColor);
  734.         }
  735.         graphics.fillArc(x, y, width, height, startAngle, arcAngle);
  736.     }
  737.  
  738.     /**
  739.      * Overrides <code>Graphics.drawPolyline</code>.
  740.      */
  741.     public void drawPolyline(int xPoints[], int yPoints[], int nPoints) {
  742.         DebugGraphicsInfo info = info();
  743.  
  744.         if (debugLog()) {
  745.             info().log(toShortString() +
  746.                       " Drawing polyline: " +
  747.                       " nPoints: " + nPoints +
  748.                       " X's: " + xPoints +
  749.                       " Y's: " + yPoints);
  750.         }
  751.         if (isDrawingBuffer()) {
  752.             if (debugBuffered()) {
  753.                 Graphics debugGraphics = debugGraphics();
  754.  
  755.                 debugGraphics.drawPolyline(xPoints, yPoints, nPoints);
  756.                 debugGraphics.dispose();
  757.             }
  758.         } else if (debugFlash()) {
  759.             Color oldColor = getColor();
  760.             int i, count = (info.flashCount * 2) - 1;
  761.  
  762.             for (i = 0; i < count; i++) {
  763.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  764.                 graphics.drawPolyline(xPoints, yPoints, nPoints);
  765.                 Toolkit.getDefaultToolkit().sync();
  766.                 sleep(info.flashTime);
  767.             }
  768.             graphics.setColor(oldColor);
  769.         }
  770.         graphics.drawPolyline(xPoints, yPoints, nPoints);
  771.     }
  772.  
  773.     /**
  774.      * Overrides <code>Graphics.drawPolygon</code>.
  775.      */
  776.     public void drawPolygon(int xPoints[], int yPoints[], int nPoints) {
  777.         DebugGraphicsInfo info = info();
  778.  
  779.         if (debugLog()) {
  780.             info().log(toShortString() +
  781.                       " Drawing polygon: " +
  782.                       " nPoints: " + nPoints +
  783.                       " X's: " + xPoints +
  784.                       " Y's: " + yPoints);
  785.         }
  786.         if (isDrawingBuffer()) {
  787.             if (debugBuffered()) {
  788.                 Graphics debugGraphics = debugGraphics();
  789.  
  790.                 debugGraphics.drawPolygon(xPoints, yPoints, nPoints);
  791.                 debugGraphics.dispose();
  792.             }
  793.         } else if (debugFlash()) {
  794.             Color oldColor = getColor();
  795.             int i, count = (info.flashCount * 2) - 1;
  796.  
  797.             for (i = 0; i < count; i++) {
  798.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  799.                 graphics.drawPolygon(xPoints, yPoints, nPoints);
  800.                 Toolkit.getDefaultToolkit().sync();
  801.                 sleep(info.flashTime);
  802.             }
  803.             graphics.setColor(oldColor);
  804.         }
  805.         graphics.drawPolygon(xPoints, yPoints, nPoints);
  806.     }
  807.  
  808.     /**
  809.      * Overrides <code>Graphics.fillPolygon</code>.
  810.      */
  811.     public void fillPolygon(int xPoints[], int yPoints[], int nPoints) {
  812.         DebugGraphicsInfo info = info();
  813.  
  814.         if (debugLog()) {
  815.             info().log(toShortString() +
  816.                       " Filling polygon: " +
  817.                       " nPoints: " + nPoints +
  818.                       " X's: " + xPoints +
  819.                       " Y's: " + yPoints);
  820.         }
  821.         if (isDrawingBuffer()) {
  822.             if (debugBuffered()) {
  823.                 Graphics debugGraphics = debugGraphics();
  824.  
  825.                 debugGraphics.fillPolygon(xPoints, yPoints, nPoints);
  826.                 debugGraphics.dispose();
  827.             }
  828.         } else if (debugFlash()) {
  829.             Color oldColor = getColor();
  830.             int i, count = (info.flashCount * 2) - 1;
  831.  
  832.             for (i = 0; i < count; i++) {
  833.                 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  834.                 graphics.fillPolygon(xPoints, yPoints, nPoints);
  835.                 Toolkit.getDefaultToolkit().sync();
  836.                 sleep(info.flashTime);
  837.             }
  838.             graphics.setColor(oldColor);
  839.         }
  840.         graphics.fillPolygon(xPoints, yPoints, nPoints);
  841.     }
  842.  
  843.     /**
  844.      * Overrides <code>Graphics.drawString</code>.
  845.      */
  846.     public void drawString(String aString, int x, int y) {
  847.         DebugGraphicsInfo info = info();
  848.  
  849.         if (debugLog()) {
  850.             info().log(toShortString() +
  851.                        " Drawing string: \"" + aString +
  852.                        "\" at: " + new Point(x, y));
  853.         }
  854.  
  855.         if (isDrawingBuffer()) {
  856.             if (debugBuffered()) {
  857.                 Graphics debugGraphics = debugGraphics();
  858.  
  859.                 debugGraphics.drawString(aString, x, y);
  860.                 debugGraphics.dispose();
  861.             }
  862.         } else if (debugFlash()) {
  863.             Color oldColor = getColor();
  864.             int i, count = (info.flashCount * 2) - 1;
  865.  
  866.             for (i = 0; i < count; i++) {
  867.                 graphics.setColor((i % 2) == 0 ? info.flashColor
  868.                                   : oldColor);
  869.                 graphics.drawString(aString, x, y);
  870.                 Toolkit.getDefaultToolkit().sync();
  871.                 sleep(info.flashTime);
  872.             }
  873.             graphics.setColor(oldColor);
  874.         }
  875.         graphics.drawString(aString, x, y);
  876.     }
  877.  
  878.     /**
  879.      * Overrides <code>Graphics.drawString</code>.
  880.      */
  881.     
  882.     public void drawString(AttributedCharacterIterator iterator, int x, int y) {
  883.         DebugGraphicsInfo info = info();
  884.  
  885.         if (debugLog()) {
  886.             info().log(toShortString() +
  887.                        " Drawing text: \"" + iterator +
  888.                        "\" at: " + new Point(x, y));
  889.         }
  890.  
  891.         if (isDrawingBuffer()) {
  892.             if (debugBuffered()) {
  893.                 Graphics debugGraphics = debugGraphics();
  894.  
  895.                 debugGraphics.drawString(iterator, x, y);
  896.                 debugGraphics.dispose();
  897.             }
  898.         } else if (debugFlash()) {
  899.             Color oldColor = getColor();
  900.             int i, count = (info.flashCount * 2) - 1;
  901.  
  902.             for (i = 0; i < count; i++) {
  903.                 graphics.setColor((i % 2) == 0 ? info.flashColor
  904.                                   : oldColor);
  905.                 graphics.drawString(iterator, x, y);
  906.                 Toolkit.getDefaultToolkit().sync();
  907.                 sleep(info.flashTime);
  908.             }
  909.             graphics.setColor(oldColor);
  910.         }
  911.         graphics.drawString(iterator, x, y);
  912.     }
  913.      
  914.  
  915.     /**
  916.      * Overrides <code>Graphics.drawBytes</code>.
  917.      */
  918.     public void drawBytes(byte data[], int offset, int length, int x, int y) {
  919.         DebugGraphicsInfo info = info();
  920.  
  921.         Font font = graphics.getFont();
  922.  
  923.         if (debugLog()) {
  924.             info().log(toShortString() +
  925.                        " Drawing bytes at: " + new Point(x, y));
  926.         }
  927.  
  928.         if (isDrawingBuffer()) {
  929.             if (debugBuffered()) {
  930.                 Graphics debugGraphics = debugGraphics();
  931.  
  932.                 debugGraphics.drawBytes(data, offset, length, x, y);
  933.                 debugGraphics.dispose();
  934.             }
  935.         } else if (debugFlash()) {
  936.             Color oldColor = getColor();
  937.             int i, count = (info.flashCount * 2) - 1;
  938.  
  939.             for (i = 0; i < count; i++) {
  940.                 graphics.setColor((i % 2) == 0 ? info.flashColor
  941.                                   : oldColor);
  942.                 graphics.drawBytes(data, offset, length, x, y);
  943.                 Toolkit.getDefaultToolkit().sync();
  944.                 sleep(info.flashTime);
  945.             }
  946.             graphics.setColor(oldColor);
  947.         }
  948.         graphics.drawBytes(data, offset, length, x, y);
  949.     }
  950.  
  951.     /**
  952.      * Overrides <code>Graphics.drawChars</code>.
  953.      */
  954.     public void drawChars(char data[], int offset, int length, int x, int y) {
  955.         DebugGraphicsInfo info = info();
  956.  
  957.         Font font = graphics.getFont();
  958.  
  959.         if (debugLog()) {
  960.             info().log(toShortString() +
  961.                        " Drawing chars at " +  new Point(x, y));
  962.         }
  963.  
  964.         if (isDrawingBuffer()) {
  965.             if (debugBuffered()) {
  966.                 Graphics debugGraphics = debugGraphics();
  967.  
  968.                 debugGraphics.drawChars(data, offset, length, x, y);
  969.                 debugGraphics.dispose();
  970.             }
  971.         } else if (debugFlash()) {
  972.             Color oldColor = getColor();
  973.             int i, count = (info.flashCount * 2) - 1;
  974.  
  975.             for (i = 0; i < count; i++) {
  976.                 graphics.setColor((i % 2) == 0 ? info.flashColor
  977.                                   : oldColor);
  978.                 graphics.drawChars(data, offset, length, x, y);
  979.                 Toolkit.getDefaultToolkit().sync();
  980.                 sleep(info.flashTime);
  981.             }
  982.             graphics.setColor(oldColor);
  983.         }
  984.         graphics.drawChars(data, offset, length, x, y);
  985.     }
  986.  
  987.     /**
  988.      * Overrides <code>Graphics.drawImage</code>.
  989.      */
  990.     public boolean drawImage(Image img, int x, int y,
  991.                              ImageObserver observer) {
  992.         DebugGraphicsInfo info = info();
  993.  
  994.         if (debugLog()) {
  995.             info().log(toShortString() +
  996.                       " Drawing image: " + img +
  997.                       " at: " + new Point(x, y));
  998.         }
  999.  
  1000.         if (isDrawingBuffer()) {
  1001.             if (debugBuffered()) {
  1002.                 Graphics debugGraphics = debugGraphics();
  1003.  
  1004.                 debugGraphics.drawImage(img, x, y, observer);
  1005.                 debugGraphics.dispose();
  1006.             }
  1007.         } else if (debugFlash()) {
  1008.             int i, count = (info.flashCount * 2) - 1;
  1009.             ImageProducer oldProducer = img.getSource();
  1010.             ImageProducer newProducer
  1011.                 = new FilteredImageSource(oldProducer,
  1012.                                 new DebugGraphicsFilter(info.flashColor));
  1013.             Image newImage
  1014.                 = Toolkit.getDefaultToolkit().createImage(newProducer);
  1015.             DebugGraphicsObserver imageObserver
  1016.                 = new DebugGraphicsObserver();
  1017.  
  1018.             for (i = 0; i < count; i++) {
  1019.                 graphics.drawImage((i % 2) == 0 ? newImage : img, x, y,
  1020.                                    imageObserver);
  1021.                 Toolkit.getDefaultToolkit().sync();
  1022.                 while (!imageObserver.allBitsPresent() &&
  1023.                                        !imageObserver.imageHasProblem()) {
  1024.                     sleep(10);
  1025.                 }
  1026.                 sleep(info.flashTime);
  1027.             }
  1028.         }
  1029.         return graphics.drawImage(img, x, y, observer);
  1030.     }
  1031.  
  1032.     /**
  1033.      * Overrides <code>Graphics.drawImage</code>.
  1034.      */
  1035.     public boolean drawImage(Image img, int x, int y, int width, int height,
  1036.                              ImageObserver observer) {
  1037.         return graphics.drawImage(img, x, y, width, height, observer);
  1038.     }
  1039.  
  1040.     /**
  1041.      * Overrides <code>Graphics.drawImage</code>.
  1042.      */
  1043.     public boolean drawImage(Image img, int x, int y,
  1044.                              Color bgcolor,
  1045.                              ImageObserver observer) {
  1046.         return graphics.drawImage(img, x, y, bgcolor, observer);
  1047.     }
  1048.  
  1049.     /**
  1050.      * Overrides <code>Graphics.drawImage</code>.
  1051.      */
  1052.     public boolean drawImage(Image img, int x, int y,int width, int height,
  1053.                              Color bgcolor,
  1054.                              ImageObserver observer) {
  1055.         return graphics.drawImage(img, x, y, width, height, bgcolor, observer);
  1056.     }
  1057.  
  1058.     /**
  1059.      * Overrides <code>Graphics.drawImage</code>.
  1060.      */
  1061.     public boolean drawImage(Image img,
  1062.                              int dx1, int dy1, int dx2, int dy2,
  1063.                              int sx1, int sy1, int sx2, int sy2,
  1064.                              ImageObserver observer) {
  1065.         return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,
  1066.                                   observer);
  1067.     }
  1068.  
  1069.     /**
  1070.      * Overrides <code>Graphics.drawImage</code>.
  1071.      */
  1072.     public boolean drawImage(Image img,
  1073.                              int dx1, int dy1, int dx2, int dy2,
  1074.                              int sx1, int sy1, int sx2, int sy2,
  1075.                              Color bgcolor,
  1076.                              ImageObserver observer) {
  1077.         return graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,
  1078.                                   bgcolor, observer);
  1079.     }
  1080.  
  1081.     /**
  1082.      * Overrides <code>Graphics.copyArea</code>.
  1083.      */
  1084.     public void copyArea(int x, int y, int width, int height,
  1085.                          int destX, int destY) {
  1086.         if (debugLog()) {
  1087.             info().log(toShortString() +
  1088.                       " Copying area from: " +
  1089.                       new Rectangle(x, y, width, height) +
  1090.                       " to: " + new Point(destX, destY));
  1091.         }
  1092.         graphics.copyArea(x, y, width, height, destX, destY);
  1093.     }
  1094.  
  1095.     final void sleep(int mSecs) {
  1096.         try {
  1097.             Thread.sleep(mSecs);
  1098.         } catch (Exception e) {
  1099.         }
  1100.     }
  1101.  
  1102.     /**
  1103.      * Overrides <code>Graphics.dispose</code>.
  1104.      */
  1105.     public void dispose() {
  1106.         graphics.dispose();
  1107.         graphics = null;
  1108.     }
  1109.  
  1110.     // ALERT!
  1111.     /**
  1112.      * Returns the drawingBuffer value.
  1113.      *
  1114.      * @return true if this object is drawing from a Buffer
  1115.      */
  1116.     public boolean isDrawingBuffer() {
  1117.         return buffer != null;
  1118.     }
  1119.  
  1120.     String toShortString() {
  1121.         StringBuffer buffer = new StringBuffer("Graphics" + (isDrawingBuffer() ? "<B>" : "") + "(" + graphicsID + "-" + debugOptions + ")");
  1122.         return buffer.toString();
  1123.     }
  1124.  
  1125.     String pointToString(int x, int y) {
  1126.         StringBuffer buffer = new StringBuffer("(" + x + ", " + y + ")");
  1127.         return buffer.toString();
  1128.     }
  1129.  
  1130.     /** Enables/disables diagnostic information about every graphics
  1131.       * operation. The value of <b>options</b> indicates how this information
  1132.       * should be displayed. LOG_OPTION causes a text message to be printed.
  1133.       * FLASH_OPTION causes the drawing to flash several times. BUFFERED_OPTION
  1134.       * creates a new Frame that shows each operation on an
  1135.       * offscreen buffer. The value of <b>options</b> is bitwise OR'd into
  1136.       * the current value. To disable debugging use NONE_OPTION.
  1137.       */
  1138.     public void setDebugOptions(int options) {
  1139.         if (options != 0) {
  1140.             if (options == NONE_OPTION) {
  1141.                 if (debugOptions != 0) {
  1142.                     System.err.println(toShortString() + " Disabling debug");
  1143.                     debugOptions = 0;
  1144.                 }
  1145.             } else {
  1146.                 if (debugOptions != options) {
  1147.                     debugOptions |= options;
  1148.                     if (debugLog()) {
  1149.                         System.err.println(toShortString() + " Enabling debug");
  1150.                     }
  1151.                 }
  1152.             }
  1153.         }
  1154.     }
  1155.  
  1156.     /** Returns the current debugging options for this DebugGraphics.
  1157.       * @see #setDebugOptions
  1158.       */
  1159.     public int getDebugOptions() {
  1160.         return debugOptions;
  1161.     }
  1162.  
  1163.     /** Static wrapper method for DebugGraphicsInfo.setDebugOptions(). Stores
  1164.       * options on a per component basis.
  1165.       */
  1166.     static void setDebugOptions(JComponent component, int options) {
  1167.         info().setDebugOptions(component, options);
  1168.     }
  1169.  
  1170.     /** Static wrapper method for DebugGraphicsInfo.getDebugOptions().
  1171.       */
  1172.     static int getDebugOptions(JComponent component) {
  1173.         DebugGraphicsInfo debugGraphicsInfo = info();
  1174.         if (debugGraphicsInfo == null) {
  1175.             return 0;
  1176.         } else {
  1177.             return debugGraphicsInfo.getDebugOptions(component);
  1178.         }
  1179.     }
  1180.  
  1181.     /** Returns non-zero if <b>component</b> should display with DebugGraphics,
  1182.       * zero otherwise. Walks the JComponent's parent tree to determine if
  1183.       * any debugging options have been set.
  1184.       */
  1185.     static int shouldComponentDebug(JComponent component) {
  1186.         DebugGraphicsInfo info = info();
  1187.         if (info == null) {
  1188.             return 0;
  1189.         } else {
  1190.             Container container = (Container)component;
  1191.             int debugOptions = 0;
  1192.  
  1193.             while (container != null && (container instanceof JComponent)) {
  1194.                 debugOptions |= info.getDebugOptions((JComponent)container);
  1195.                 container = container.getParent();
  1196.             }
  1197.  
  1198.             return debugOptions;
  1199.         }
  1200.     }
  1201.  
  1202.     /** Returns the number of JComponents that have debugging options turned
  1203.       * on.
  1204.       */
  1205.     static int debugComponentCount() {
  1206.         DebugGraphicsInfo debugGraphicsInfo = info();
  1207.         if (debugGraphicsInfo != null &&
  1208.                     debugGraphicsInfo.componentToDebug != null) {
  1209.             return debugGraphicsInfo.componentToDebug.size();
  1210.         } else {
  1211.             return 0;
  1212.         }
  1213.     }
  1214.  
  1215.     boolean debugLog() {
  1216.         return (debugOptions & LOG_OPTION) == LOG_OPTION;
  1217.     }
  1218.  
  1219.     boolean debugFlash() {
  1220.         return (debugOptions & FLASH_OPTION) == FLASH_OPTION;
  1221.     }
  1222.  
  1223.     boolean debugBuffered() {
  1224.         return (debugOptions & BUFFERED_OPTION) == BUFFERED_OPTION;
  1225.     }
  1226.  
  1227.     /** Returns a DebugGraphics for use in buffering window.
  1228.       */
  1229.     private Graphics debugGraphics() {
  1230.         DebugGraphics        debugGraphics;
  1231.         DebugGraphicsInfo    info = info();
  1232.         JFrame               debugFrame;
  1233.  
  1234.         if (info.debugFrame == null) {
  1235.             info.debugFrame = new JFrame();
  1236.             info.debugFrame.setSize(500, 500);
  1237.         }
  1238.         debugFrame = info.debugFrame;
  1239.         debugFrame.show();
  1240.         debugGraphics = new DebugGraphics(debugFrame.getGraphics());
  1241.         debugGraphics.setFont(getFont());
  1242.         debugGraphics.setColor(getColor());
  1243.         debugGraphics.translate(xOffset, yOffset);
  1244.         debugGraphics.setClip(getClipBounds());
  1245.         if (debugFlash()) {
  1246.             debugGraphics.setDebugOptions(FLASH_OPTION);
  1247.         }
  1248.         return debugGraphics;
  1249.     }
  1250.  
  1251.     /** Returns DebugGraphicsInfo, or creates one if none exists.
  1252.       */
  1253.     static DebugGraphicsInfo info() {
  1254.         DebugGraphicsInfo debugGraphicsInfo = (DebugGraphicsInfo)
  1255.             SwingUtilities.appContextGet(debugGraphicsInfoKey);
  1256.         if (debugGraphicsInfo == null) {
  1257.             debugGraphicsInfo = new DebugGraphicsInfo();
  1258.             SwingUtilities.appContextPut(debugGraphicsInfoKey,
  1259.                                          debugGraphicsInfo);
  1260.         }
  1261.         return debugGraphicsInfo;
  1262.     }
  1263.     private static final Class debugGraphicsInfoKey = DebugGraphicsInfo.class;
  1264.  
  1265.     static {
  1266.         // Warn if running wrong version of this class for this JDK.
  1267.         
  1268.           if (!SwingUtilities.is1dot2) {
  1269.               System.err.println("warning: running 1.2 version of DebugGraphics");
  1270.           }
  1271.           
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.     }
  1281. }
  1282.